<?php

namespace apiend\modules\setting\models;

use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Url;
use common\models\book\Stage;

/**
 * ShopBrandLogic is the model class for table "{{%shop_brand}}".
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class StageSearch extends Stage {

    /**
     * @inheritdoc
     */
    public function typecastAttributes() {
        return [
            parent::EVENT_AFTER_FIND => [
//                [
//                    'attributes' => ['thumb', 'banner', 'logo'],
//                    'type' => function ($event, $attribute) {
//                        return Url::attach($this->$attribute);
//                    },
//                ],
            ]
        ];
    }

    /**
     * @var string 关键字
     */
    public $keyword;

    /**
     * @var status 状态
     */
    public $statusList;

    /**
     * @var status 状态
     */
    public $sort = [
        'defaultOrder' => [
            'created_at' => SORT_DESC,
        ]
    ];

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword', 'statusList'], 'safe'],
        ];
    }

    public function getActiveStatus() {
        $status = [
            0 => '未启用',
            10 => '已启用',
        ];
        return ArrayHelper::getValue($status, $this->statusList);
    }

    /**
     * Searching member apply
     * @param  array $params
     * @return \yii\data\ActiveDataProvider
     */
    public function buildQuery($params = []) {
        $query = static::find()->active();
        if (!empty($params)) {
            $query->andFilterWhere($params);
        }
        if ($this->keyword) {
            $query->andFilterWhere([
                'or',
                ['like', 'name', $this->keyword],
            ]);
        }
        if ($this->activeStatus) {
            $query->andFilterWhere(['status' => $this->statusList]);
        }
        return $query;
    }

    /**
     * {@inheritdoc}
     */
    public function fieldConfigs() {
        return [
            [
                'attribute' => 'id',
                'headerOptions' => ['width' => 80],
            ],
            'name',
            [
                'label' => '开始时间',
                'attribute' => 'startHour',
                'headerOptions' => ['width' => 150],
            ],
            [
                'label' => '截止时间',
                'attribute' => 'endHour',
                'headerOptions' => ['width' => 150],
            ],
            [
                'class' => 'common\rest\grid\LabelColumn',
                'attribute' => 'status',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'updated_at',
                'format' => 'datetimeRow',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'class' => 'common\rest\grid\ActionColumn',
                'template' => '{update}{delete}',
                'headerOptions' => ['width' => '120'],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function configs() {
        return [
            'dialog' => [
                'visible' => false,
                'title' => '参观时段设置'
            ],
            'config' => [
                'gridConfig' => $this->options(),
            ]
        ];
    }

}
